package com.evolution.warlock.dao.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.FlushModeType;
import javax.persistence.NoResultException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;





import org.springframework.stereotype.Repository;

import com.evolution.warlock.Page;
import com.evolution.warlock.Pageable;
import com.evolution.warlock.dao.CriminalBaseDao;
import com.evolution.warlock.entity.Admin;
import com.evolution.warlock.entity.CriminalBase;
import com.evolution.warlock.entity.MasterConfig;

@Repository("criminalBaseDaoImpl")
public class CriminalBaseDaoImpl extends BaseDaoImpl<CriminalBase, Long>
		implements CriminalBaseDao {

	/*
	 * (non-Javadoc)
	 * @see com.evolution.warlock.dao.CriminalBaseDao#findPage(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 * 
	 * 根据具体字段查询
	 * 
	 */
	@Override
	public Page<CriminalBase> findPage(Pageable pageable, String idCardNum, String name, String criminalCategory, String penaltyCategory, String temporaryGuy, Collection<String> currentArea, Date fromDate, Date toDate) {
		// TODO Auto-generated method stub
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<CriminalBase> criteriaQuery = criteriaBuilder
				.createQuery(CriminalBase.class);
		Root<CriminalBase> root = criteriaQuery.from(CriminalBase.class);
		criteriaQuery.select(root);
		Predicate restrictions = criteriaBuilder.conjunction();

		if (null != idCardNum) {
			restrictions = criteriaBuilder.and(restrictions,
					criteriaBuilder.equal(root.get("idCardNum"), idCardNum));
		}
		if(null != name){
			restrictions = criteriaBuilder.and(restrictions,criteriaBuilder.like(root.<String>get("name"),"%" + name + "%"));
		}
		if (null != criminalCategory) {
			restrictions = criteriaBuilder.and(restrictions,criteriaBuilder.like(root.<String>get("caseCriminal"),"%" + criminalCategory + "%"));
		}
		if (null != penaltyCategory) {
			restrictions = criteriaBuilder.and(restrictions,criteriaBuilder.like(root.<String>get("casePenalty"), "%" + penaltyCategory + "%"));
		}
		if (null != temporaryGuy) {
			restrictions = criteriaBuilder.and(restrictions, criteriaBuilder
					.equal(root.get("temporaryGuy"), temporaryGuy));
		}
		if(null != currentArea){
			restrictions = criteriaBuilder.and(restrictions,
					criteriaBuilder.isTrue(root.get("currentArea").in(currentArea)));
		}
		if(null != fromDate){
			restrictions = criteriaBuilder.and(restrictions,
					criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"),fromDate));
		}
		if(null != toDate){
			restrictions = criteriaBuilder.and(restrictions,
					criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"),toDate));
		}
		restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("status"), "0"));
		criteriaQuery.where(restrictions);
		return super.findPage(criteriaQuery, pageable);
	}

	@Override
	public CriminalBase findCriminalBaseByIdCardNum(String idCardNum) {
		// TODO Auto-generated method stub
		if (idCardNum == null) {
			return null;
		}
		try {
			String jpql = "select criminal from CriminalBase criminal where lower(criminal.idCardNum) = lower(:idCardNum)";
			return entityManager.createQuery(jpql, CriminalBase.class).setFlushMode(FlushModeType.COMMIT).setParameter("idCardNum", idCardNum).getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	public CriminalBase findCriminalBase(Long criminalNumber) {
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<CriminalBase> criteriaQuery = criteriaBuilder.createQuery(CriminalBase.class);
		Root<CriminalBase> root = criteriaQuery.from(CriminalBase.class);
		criteriaQuery.select(root);
		Predicate restrictions = criteriaBuilder.conjunction();
		if(criminalNumber != null){
		   restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("criminalNumber"), criminalNumber));
		}
	    criteriaQuery.where(restrictions);
	    List<CriminalBase> criminalBaseList = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
	    if(null!=criminalBaseList && criminalBaseList.size()>0){
	    	return criminalBaseList.get(0);
	    }else{
	    	return null;
	    }
	}
	
	
}
